{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas的SettingWithCopyWarning报警"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 0、读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "fpath = \"./datas/beijing_tianqi/beijing_tianqi_2018.csv\"\n",
    "df = pd.read_csv(fpath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>3℃</td>\n",
       "      <td>-6℃</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>59</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-5℃</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>49</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-5℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>0℃</td>\n",
       "      <td>-8℃</td>\n",
       "      <td>阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>3℃</td>\n",
       "      <td>-6℃</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd bWendu yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2018-01-01     3℃    -6℃   晴~多云       东北风   1-2级   59       良         2\n",
       "1  2018-01-02     2℃    -5℃   阴~多云       东北风   1-2级   49       优         1\n",
       "2  2018-01-03     2℃    -5℃     多云        北风   1-2级   28       优         1\n",
       "3  2018-01-04     0℃    -8℃      阴       东北风   1-2级   28       优         1\n",
       "4  2018-01-05     3℃    -6℃   多云~晴       西北风   1-2级   50       优         1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 替换掉温度的后缀℃\n",
    "df.loc[:, \"bWendu\"] = df[\"bWendu\"].str.replace(\"℃\", \"\").astype('int32')\n",
    "df.loc[:, \"yWendu\"] = df[\"yWendu\"].str.replace(\"℃\", \"\").astype('int32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>59</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>49</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>2</td>\n",
       "      <td>-5</td>\n",
       "      <td>多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>0</td>\n",
       "      <td>-8</td>\n",
       "      <td>阴</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>28</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>3</td>\n",
       "      <td>-6</td>\n",
       "      <td>多云~晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2018-01-01       3      -6   晴~多云       东北风   1-2级   59       良         2\n",
       "1  2018-01-02       2      -5   阴~多云       东北风   1-2级   49       优         1\n",
       "2  2018-01-03       2      -5     多云        北风   1-2级   28       优         1\n",
       "3  2018-01-04       0      -8      阴       东北风   1-2级   28       优         1\n",
       "4  2018-01-05       3      -6   多云~晴       西北风   1-2级   50       优         1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、复现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只选出3月份的数据用于分析\n",
    "condition = df[\"ymd\"].str.startswith(\"2018-03\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\appdata\\python37\\lib\\site-packages\\ipykernel_launcher.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "# 设置温差\n",
    "df[condition][\"wen_cha\"] = df[\"bWendu\"]-df[\"yWendu\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>59</td>\n",
       "      <td>2018-03-01</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>多云</td>\n",
       "      <td>西南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>46</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>60</td>\n",
       "      <td>2018-03-02</td>\n",
       "      <td>9</td>\n",
       "      <td>-1</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>95</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>61</td>\n",
       "      <td>2018-03-03</td>\n",
       "      <td>13</td>\n",
       "      <td>3</td>\n",
       "      <td>多云~阴</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>214</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>62</td>\n",
       "      <td>2018-03-04</td>\n",
       "      <td>7</td>\n",
       "      <td>-2</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>144</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>63</td>\n",
       "      <td>2018-03-05</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>94</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "59  2018-03-01       8      -3     多云       西南风   1-2级   46       优         1\n",
       "60  2018-03-02       9      -1   晴~多云        北风   1-2级   95       良         2\n",
       "61  2018-03-03      13       3   多云~阴        北风   1-2级  214    重度污染         5\n",
       "62  2018-03-04       7      -2   阴~多云       东南风   1-2级  144    轻度污染         3\n",
       "63  2018-03-05       8      -3      晴        南风   1-2级   94       良         2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看是否修改成功\n",
    "df[condition].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、原因\n",
    "发出警告的代码\n",
    "df[condition][\"wen_cha\"] = df[\"bWendu\"]-df[\"yWendu\"]\n",
    "\n",
    "相当于：df.get(condition).set(wen_cha)，第一步骤的get发出了报警\n",
    "\n",
    "***链式操作其实是两个步骤，先get后set，get得到的dataframe可能是view也可能是copy，pandas发出警告***\n",
    "\n",
    "官网文档：\n",
    "https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
    "\n",
    "核心要诀：pandas的dataframe的修改写操作，只允许在源dataframe上进行，一步到位"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、解决方法1\n",
    "\n",
    "将get+set的两步操作，改成set的一步操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc[condition, \"wen_cha\"] = df[\"bWendu\"]-df[\"yWendu\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "      <th>wen_cha</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>59</td>\n",
       "      <td>2018-03-01</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>多云</td>\n",
       "      <td>西南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>46</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>60</td>\n",
       "      <td>2018-03-02</td>\n",
       "      <td>9</td>\n",
       "      <td>-1</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>95</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>61</td>\n",
       "      <td>2018-03-03</td>\n",
       "      <td>13</td>\n",
       "      <td>3</td>\n",
       "      <td>多云~阴</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>214</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>62</td>\n",
       "      <td>2018-03-04</td>\n",
       "      <td>7</td>\n",
       "      <td>-2</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>144</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>3</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>63</td>\n",
       "      <td>2018-03-05</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>94</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel  \\\n",
       "59  2018-03-01       8      -3     多云       西南风   1-2级   46       优         1   \n",
       "60  2018-03-02       9      -1   晴~多云        北风   1-2级   95       良         2   \n",
       "61  2018-03-03      13       3   多云~阴        北风   1-2级  214    重度污染         5   \n",
       "62  2018-03-04       7      -2   阴~多云       东南风   1-2级  144    轻度污染         3   \n",
       "63  2018-03-05       8      -3      晴        南风   1-2级   94       良         2   \n",
       "\n",
       "    wen_cha  \n",
       "59     11.0  \n",
       "60     10.0  \n",
       "61     10.0  \n",
       "62      9.0  \n",
       "63     11.0  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[condition].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、解决方法2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果需要预筛选数据做后续的处理分析，使用copy复制dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_month3 = df[condition].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "      <th>wen_cha</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>59</td>\n",
       "      <td>2018-03-01</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>多云</td>\n",
       "      <td>西南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>46</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>60</td>\n",
       "      <td>2018-03-02</td>\n",
       "      <td>9</td>\n",
       "      <td>-1</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>95</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>61</td>\n",
       "      <td>2018-03-03</td>\n",
       "      <td>13</td>\n",
       "      <td>3</td>\n",
       "      <td>多云~阴</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>214</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>62</td>\n",
       "      <td>2018-03-04</td>\n",
       "      <td>7</td>\n",
       "      <td>-2</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>144</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>3</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>63</td>\n",
       "      <td>2018-03-05</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>94</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel  \\\n",
       "59  2018-03-01       8      -3     多云       西南风   1-2级   46       优         1   \n",
       "60  2018-03-02       9      -1   晴~多云        北风   1-2级   95       良         2   \n",
       "61  2018-03-03      13       3   多云~阴        北风   1-2级  214    重度污染         5   \n",
       "62  2018-03-04       7      -2   阴~多云       东南风   1-2级  144    轻度污染         3   \n",
       "63  2018-03-05       8      -3      晴        南风   1-2级   94       良         2   \n",
       "\n",
       "    wen_cha  \n",
       "59     11.0  \n",
       "60     10.0  \n",
       "61     10.0  \n",
       "62      9.0  \n",
       "63     11.0  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_month3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_month3[\"wen_cha\"] = df[\"bWendu\"]-df[\"yWendu\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "      <th>wen_cha</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>59</td>\n",
       "      <td>2018-03-01</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>多云</td>\n",
       "      <td>西南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>46</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>60</td>\n",
       "      <td>2018-03-02</td>\n",
       "      <td>9</td>\n",
       "      <td>-1</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>95</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>61</td>\n",
       "      <td>2018-03-03</td>\n",
       "      <td>13</td>\n",
       "      <td>3</td>\n",
       "      <td>多云~阴</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>214</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>62</td>\n",
       "      <td>2018-03-04</td>\n",
       "      <td>7</td>\n",
       "      <td>-2</td>\n",
       "      <td>阴~多云</td>\n",
       "      <td>东南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>144</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>63</td>\n",
       "      <td>2018-03-05</td>\n",
       "      <td>8</td>\n",
       "      <td>-3</td>\n",
       "      <td>晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>94</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel  \\\n",
       "59  2018-03-01       8      -3     多云       西南风   1-2级   46       优         1   \n",
       "60  2018-03-02       9      -1   晴~多云        北风   1-2级   95       良         2   \n",
       "61  2018-03-03      13       3   多云~阴        北风   1-2级  214    重度污染         5   \n",
       "62  2018-03-04       7      -2   阴~多云       东南风   1-2级  144    轻度污染         3   \n",
       "63  2018-03-05       8      -3      晴        南风   1-2级   94       良         2   \n",
       "\n",
       "    wen_cha  \n",
       "59       11  \n",
       "60       10  \n",
       "61       10  \n",
       "62        9  \n",
       "63       11  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_month3.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***总之，pandas不允许先筛选子dataframe，再进行修改写入***  \n",
    "要么使用.loc实现一个步骤直接修改源dataframe  \n",
    "要么先复制一个子dataframe再一个步骤执行修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
